﻿package util {
	import flash.text.TextField;
	
	public class GetBlockIndex {
		//获取光标文字的左右索引
		public static function getTextLeft(tf:TextField):int {
			if (tf.selectionBeginIndex > tf.selectionEndIndex) {
				return tf.selectionEndIndex;
			}
			else {
				return tf.selectionBeginIndex;
			}
		}
		public static function getTextRight(tf:TextField):int {
			if (tf.selectionBeginIndex > tf.selectionEndIndex) {
				return tf.selectionBeginIndex;
			}
			else {
				return tf.selectionEndIndex;
			}
		}
		//获取光标文字所对应的html代码中左右索引
		public static function getHtmlLeft(s:String, left:int):int {
			//将left索引转换为对应的html索引，<>标记中不计数
			var makeupSwitch:Boolean = false;
			var count:int = -1;
			for (var i:int = 0; i < s.length; i++) {
				var c:String = s.charAt(i);
				if (c == "<") {
					makeupSwitch = false;
					continue;
				}
				else if (c == ">") {
					makeupSwitch = true;
					continue;
				}
				if (makeupSwitch && (++count == left)) {
					return i;
				}
			}
			return 0;
		}
		public static function getHtmlRight(s:String, right:int, left:int = 0, htmlLeft:int = 0):int {
			//将right索引转换为对应的html索引，<>标记中不计数，right为内边界
			var makeupSwitch:Boolean = true;
			var count:int = -1, result:int = right - left - 1;
			for (var i:int = htmlLeft; i < s.length; i++) {
				var c:String = s.charAt(i);
				if (c == "<") {
					makeupSwitch = false;
					continue;
				}
				else if (c == ">") {
					makeupSwitch = true;
					continue;
				}
				if (makeupSwitch && (++count == result)) {
					return i + 1;
				}
			}
			return s.length;
		}
		//获取biu光标所选区域对应的html代码中索引数组
		public static function getBIUHtmlBlock(s:String, left:int, right:int, tag:String = null, setOrCancel:Boolean = true):Array {
			//两个数组分别装入正反区间索引标记
			var forward:Array = new Array();
			var convert:Array = new Array();
			forward.push(left);
			//反转标识、标记分析开关、存入数组标识开关以及计数器
			var reverse:int = 0;
			var makeupSwitch:Boolean = true, toggle:Boolean = true;
			var count:int = 0;
			//遍历进行索引分析
			for (var i:int = 0; i < s.length; i++) {
				var c:String = s.charAt(i);
				if (c == "<") {
					//在文本和html交界处根据toggle状态存入索引到两个数组中的一个
					if (s.charAt(i - 1) != ">") {
						if(toggle) {
							forward.push(left + count);
						}
						else {
							convert.push(left + count);
						}
					}
					//遇到tag标签每次切换状态
					if(tag != null && (s.charAt(i + 1) == tag || s.charAt(i + 2) == tag)) {
						toggle = toggle ? false : true;
						//检查是否反转
						if (reverse == 0) {
							if (s.charAt(i + 1) == tag) {
								reverse = 1;
							}
							else {
								reverse = -1;
							}
						}
					}
					makeupSwitch = false;
					continue;
				}
				else if (c == ">") {
					if(s.charAt(i + 1) != "<") {
						if(toggle) {
							forward.push(left + count);
						}
						else {
							convert.push(left + count);
						}
					}
					makeupSwitch = true;
					continue;
				}
				if (makeupSwitch) {
					count++;
				}
			}
			//结尾部分根据toogle存入
			if (toggle) {
				forward.push(right);
			}
			else {
				convert.push(right);
			}
			//是设置状态还是取消状态
			if (setOrCancel) {
				//根据反转状态返回两个数组中的一个
				if (reverse == -1) {
					return convert;
				}
				else {
					return forward;
				}
			}
			else {
				if (reverse == 1) {
					return convert;
				}
				else {
					return forward;
				}
			}
		}
		//获取其它的html块
		/*public static function getHtmlBlock(s:String, left:int, right:int):Array {
			var res:Array = new Array();
			res.push(left);
			//<>开关和计数器
			var makeupSwitch:Boolean = true;
			var count:int = 0;
			//遍历分析
			for (var i:int = 0; i < s.length; i++) {
				var c:String = s.charAt(i);
				if (c == "<") {
					//在文本和html交界处根据makeupSwitch状态存入索引
					if (s.charAt(i - 1) != ">") {
						res.push(left + count);
					}
					makeupSwitch = false;
					continue;
				}
				else if (c == ">") {
					if(s.charAt(i + 1) != "<") {
						res.push(left + count);
					}
					makeupSwitch = true;
					continue;
				}
				if (makeupSwitch) {
					count++;
				}
			}
			res.push(right);
			return res;
		}*/
	}
}